package com.cogent.dynamicform.form.impl;

import java.util.HashMap;

import com.cogent.common.util.MapUtil;
import com.cogent.common.util.StringUtil;
import com.cogent.dynamicform.domain.BaseFormColumnDomain;
import com.cogent.dynamicform.domain.FormColumnDomain;
import com.cogent.dynamicform.form.AbstractDynamicForm;
import com.cogent.dynamicform.util.DynamicFormBusUtil;

public class MutiImgUploadDynamicFormImpl extends AbstractDynamicForm {
	public String genQueryField(BaseFormColumnDomain formColumnDomain, HashMap formMap) {
		return "";
	}
	
	public String genInputField(FormColumnDomain formColumnDomain, HashMap formMap) {
		if(formColumnDomain.getViewmodel() > 0)
			return getShowValue(formColumnDomain, formMap);
		String src = MapUtil.getStringValue(formMap, formColumnDomain.getName());		
		String visiable = "";
		if(StringUtil.isEmpty(src))
			visiable = "visibility:hidden;";
		String showclass = " width: 120px; height: 130px;";
		if(!StringUtil.isEmpty(formColumnDomain.getShowclass()))
			showclass = formColumnDomain.getShowclass();
		String jscontrol = " onload=\"if(this.width>100) this.width=100;if(this.height>120) this.height=120;\"";
		if(!StringUtil.isEmpty(formColumnDomain.getJs()))
			jscontrol = formColumnDomain.getJs();
		StringBuffer str = new StringBuffer();
		if(!StringUtil.isEmpty(src)) {
			String[] srcs = src.split(";");
			for(int i=0; i<srcs.length; i++) {
				str.append("<div id=\"div_photoview").append(i).append("\" style=\" ").append(" overflow: hidden; float:left\">")
					.append("<img  src=\"").append(srcs[i]).append("\" class=\"photo\" style=\"").append(showclass)
					 .append("\" />")
					.append("<img src=\"images/btn_clear.gif\" alt=\"清空\" width=\"22\" height=\"22\" style=\"cursor: hand;\" onClick=\"clearphotoviewdiv('div_photoview"+i+"',"+i+");\" border=\"0\" align=\"absmiddle\">")
					.append("</div>");
			}
			src += ";";
		}
		str.append("<div id=\"selectdiv\" ><div id=\"showphoto\" style='float:left'><div id=\"div_photo\"").append(" style=\"").append(visiable).append(showclass).append("overflow: hidden;\">")
			.append("<div id=\"forIE7\" style=\"filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale);\"></div>")
			.append("</div><div id=\"imgareaid\" style=\"width: 55px; height: 20px; border: none; padding: 1px 2px 1px 0px; \" title=\"选择图片\">")
			.append("<input type=\"file\" name=\"upload\" style=\"width: 70px; border: none; margin: -1px;\" onchange=\"showPhoto2(this,'div_photo','img','forIE7', 'gif,jpg,jpeg,png,bmp');\"/>")
			.append("</div></div></div>");
		str.append("<div class='s_btn'><input  type=\"button\" value=\"添加\" onclick=\"addUpload('selectdiv')\"/></div>");
	    str.append("<input type=\"hidden\" name=\"muti_photo_name\" id=\"muti_photo_name\" value=\"").append(src).append("\")/>");
	    if(!StringUtil.isEmpty(formColumnDomain.getNotemsg()))
			str.append(formColumnDomain.getNotemsg());
	    
	    return str.toString();
	}
	
	public String getShowValue(BaseFormColumnDomain formColumnDomain, HashMap formMap) {
		String src = MapUtil.getStringValue(formMap, formColumnDomain.getName());
		StringBuffer str = new StringBuffer();
		String showclass = " width: 120px; height: 130px;";
		if(!StringUtil.isEmpty(formColumnDomain.getShowclass()))
			showclass = formColumnDomain.getShowclass();
		String jscontrol = " onload=\"if(this.width>100) this.width=100;if(this.height>120) this.height=120;\"";
		if(!StringUtil.isEmpty(formColumnDomain.getJs()))
			jscontrol = formColumnDomain.getJs();
		if(!StringUtil.isEmpty(src)) {
			str.append("<STYLE>	"	+
					     "IMG {" +
					"				BORDER-BOTTOM: medium none; BORDER-LEFT: medium none; BORDER-TOP: medium none; BORDER-RIGHT: medium none" +
					"			}" +
					"			.txt_1 {" +
					"				FONT: bold 24px Verdana, Tahoma; COLOR: #fff" +
					"			}" +
					"			IMG.thumb_img {" +
					"				DISPLAY: block; MARGIN-BOTTOM: 10px; CURSOR: pointer" +
					"			}" +
					"			IMG#main_img {" +
					"				DISPLAY: block; CURSOR: pointer" +
					"			}" +
					"			#gotop {" +
					"				DISPLAY: block; CURSOR: pointer" +
					"			}" +
					"			#gobottom {" +
					"				DISPLAY: block; CURSOR: pointer" +
					"			}" +
					"			#showArea {" +
					"				MARGIN: 10px; HEIGHT: 355px; OVERFLOW: hidden" +
					"			}" +
					"			.info {" +
					"				MARGIN-TOP: 20px; FONT: 9px Verdana; COLOR: #666" +
					"			}" +
					"			.info A:link {" +
					"				COLOR: #666; TEXT-DECORATION: none" +
					"			}" +
					"			.info A:visited {" +
					"				COLOR: #666; TEXT-DECORATION: none" +
					"			}" +
					"			.info A:hover {" +
					"				COLOR: #fff; TEXT-DECORATION: none" +
					"			}" +
					"			</STYLE>");
                String[] srcs = src.split(";");
			str.append("<TABLE border=0 cellSpacing=5 cellPadding=0 width=760 align=center><TBODY>" +
					"			<TD width=640 align=middle><IMG id=main_img border=0  src=\""+srcs[0]+"\" width=640 height=400 rel=\""+srcs[0]+"\"></TD>" +
					"					<TD vAlign=top width=110 align=middle><IMG id=gotop src=\"images/gotop.gif\" width=100 height=14> " +
					"			<DIV id=showArea>");  
					for(int i=0; i<srcs.length; i++) {
			    	str .append("<img  id=\"img\" src=\"").append(srcs[i]).append("\" class=\"thumb_img\" width=\"80\" height=\"50\" border=\"0\"  rel=\"").append(srcs[i]).append("\" ")
					.append("/>");
					}
					str.append("</div>").append("<IMG id=gobottom src=\"images/gobottom.gif\" width=100 height=14></TD></TR></TBODY></TABLE>"); 
			

			str.append("<SCRIPT language=\"javascript\" type=\"text/javascript\"> " +
					"		function $(e) {return document.getElementById(e);}" +
					"			document.getElementsByClassName = function(cl) {" +
					"			    var retnode = [];" +
					"			    var myclass = new RegExp('\\\\b'+cl+'\\\\b');" +
					"			    var elem = this.getElementsByTagName('*');" +
					"			    for (var i = 0; i < elem.length; i++) {" +
					"			        var classes = elem[i].className;" +
					"			        if (myclass.test(classes)) retnode.push(elem[i]);" +
					"			    }" +
					"			    return retnode;" +
					"			};" +
					"			var MyMar;" + 
					"			var thumbs = document.getElementsByClassName('thumb_img');" +
					"			for (var i=0; i<thumbs.length; i++) {" +
					"			    thumbs[i].onmouseover = function () {$('main_img').src=this.rel; };" +
					"			   " +
					"			};" +
					"			$('gotop').onmouseover = function() {this.src = 'images/gotop2.gif'; MyMar=setInterval(gotop,1);};" +
					"			$('gotop').onmouseout = function() {this.src = 'images/gotop.gif'; clearInterval(MyMar);};" +
					"			$('gobottom').onmouseover = function() {this.src = 'images/gobottom2.gif'; MyMar=setInterval(gobottom,1);};" +
					"			$('gobottom').onmouseout = function() {this.src = 'images/gobottom.gif'; clearInterval(MyMar);};" +
					"			function gotop() {$('showArea').scrollTop-=1;};" +
					"			function gobottom() {$('showArea').scrollTop+=1;};" +
					"			</SCRIPT>"); 
			}else {
			str.append("<div id=\"div_photo\" ").append(showclass).append(" overflow: hidden\">");
			str.append("</div>");
		}
		
		return str.toString();
	}
	
	public void dealFormValue(FormColumnDomain formColumnDomain, HashMap formMap) {
		String typecode = formColumnDomain.getTypecode();
		String curFile = MapUtil.getStringValue(formMap, "muti_photo_name");
		if(!MapUtil.isEmpty(formMap, DynamicFormBusUtil.DYNAMICFORM_FILPATH)) {
			if(StringUtil.isEmpty(curFile))
				curFile = MapUtil.getStringValue(formMap, DynamicFormBusUtil.DYNAMICFORM_FILPATH);
			else
				curFile += ";" + MapUtil.getStringValue(formMap, DynamicFormBusUtil.DYNAMICFORM_FILPATH);
		}
		if(StringUtil.isEmpty(typecode)) {
			formMap.put("imgsrc", curFile);
			formMap.put("photosrc", curFile);
		} else 
			formMap.put(typecode, curFile);
	}
}